﻿using System;
using TMPro;
using UnityEngine;

namespace NettyDeomo.entity
{
    public class Player:MonoBehaviour
    {
        private int id;
        public bool isMainPlayer = false;
        public Vector2 moveTarget = Vector2.zero;
        public bool isMoving = false;
        public float speed = 7;
        
        public Vector3 moveDir = Vector3.zero;
        public TextMeshPro idText;
        public TextMeshPro posText;
        public TextMeshPro speedText;
        public TextMeshPro moveDirText;

        public int Id
        {
            get => id;
            set
            {
                id = value;
                if (idText != null)
                {
                    idText.text = id + "";
                }
            }
        }

        private void Start()
        {

        }

        private void Update()
        {
            posText.text = transform.position.x.ToString("#0.00") + ":" + transform.position.z.ToString("#0.00");
            speedText.text = speed + "";
            moveDirText.text = moveDir.x + ":" + moveDir.z;
            
            if (isMoving)
            {
                if (moveDir.Equals(Vector3.zero) || speed == 0)
                {
                    isMoving = false;
                }
                // Vector3 pos = Vector3.MoveTowards(transform.position, new Vector3(moveTarget.x, 0, moveTarget.y),  speed * Time.deltaTime);

                // if (Vector3.Distance(pos, moveTarget) < 0.1f)
                // {
                //     isMoving = false;
                // }
                transform.Translate(moveDir*Time.deltaTime*speed,Space.World);
            }
            
        }

        public void localMove(Vector3 direction)
        {
            transform.Translate(direction*Time.deltaTime*speed,Space.World);
        }
    }
}